﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Linq;
using aier.business.logic;
using aier.dataaccess.model;

using log4net;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 筛查结果
    /// </summary>
    [RoutePrefix("api/ScreenResult")]
    public class ScreenResultController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }
        /// <summary>
        /// 筛查组逻辑
        /// </summary>
        public ScreenResultBll screenResultBll { get; set; }

        public CommonBll commonBll { get; set; }
        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }

        /// <summary>
        /// 创建筛查
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// 原程序 :ScResultServiceImpl-> save(TScResult entity)
        /// </summary>
        /// <param name="result">筛查结果对像</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Post([FromBody]  ScreenResult result)
        {
            try
            {

                ResResult resResult = screenResultBll.CreateScreenResult(result);
                return Ok<ResResult>(resResult);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }



        /// <summary>
        /// 更新筛查结果
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// 更新操作一定需要带上主键
        /// 原程序 :ScResultServiceImpl-> update(TScResult entity)
        /// </summary>
        /// <param name="result">筛查结果对像</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Put([FromBody]  ScreenResult result)
        {
            try
            {
                if (!Guid.Empty.Equals(result.new_sc_resultId))
                {
                    ResResult resResult = screenResultBll.UpdateScreenResult(result);
                    return Ok<ResResult>(resResult);
                }

                return NotFound();

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }



        /// <summary>
        /// 批量更新筛查结果
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// 原程序 :ScResultServiceImpl-> saveBatch(List TScResult  scResults)
        /// </summary>
        /// <param name="results">筛查结果对像列表</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("BatchUpdateResult")]
        public IHttpActionResult BatchUpdateResult([FromBody] IEnumerable<ScreenResult> results)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid userid = new Guid(userInfo.userid.ToString());
                if (results != null && results.Count() > 0)
                {
                    foreach (ScreenResult result in results)
                    {
                        result.userid = userid;
                        result.new_doctorid = userid;
                    }
                }
                else
                {
                    return new ApiErrorResult("传入筛查结果列表为空", Request);
                }

                ResResult resResult = screenResultBll.MutipleUpdateResultParallel(results);
                return Ok<ResResult>(resResult);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
       

        /// <summary>
        /// 批量创建筛查结果
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// 原程序 :ScResultServiceImpl-> saveBatch(List TScResult  scResults)
        /// </summary>
        /// <param name="results">筛查结果对像列表</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("BatchCreateResult")]
        public IHttpActionResult PostBatch([FromBody] IEnumerable<ScreenResult> results)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid userid = new Guid(userInfo.userid.ToString());
                if (results != null&&results.Count()>0)
                {
                    foreach (ScreenResult result in results)
                    {
                        result.userid = userid;
                        result.new_doctorid = userid;
                        result.OwnerId = userid;
                    }
                }
                else {
                    return new ApiErrorResult("传入筛查结果列表为空", Request);
                }
               
                ResResult resResult = screenResultBll.MutipleParallelCreateResult(results);
                return Ok<ResResult>(resResult);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 批量创建筛查结果(为满足校园筛查活动)
        /// 如果对像中userid有值，则模仿userid用户操作，反之使用默认账号操作
        /// 原程序 :ScResultServiceImpl-> saveBatch(List TScResult  scResults)
        /// </summary>
        /// <param name="results">筛查结果对像列表</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("PostBatchScreenResult")]
        public IHttpActionResult PostBatchScreenResult(ScreenResultRequest screenResultRequest)
        {
            try
            {
                if (screenResultRequest != null)
                {
                    UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                    Guid userid = new Guid(userInfo.userid.ToString());
                    if (screenResultRequest.screenCustomer!=null) {
                        screenResultRequest.screenCustomer.userid = userid;
                    }
                    if (screenResultRequest.screenResults != null && screenResultRequest.screenResults.Count() > 0)
                    {
                        foreach (ScreenResult result in screenResultRequest.screenResults)
                        {
                            result.userid = userid;
                            result.new_doctorid = userid;
                            result.OwnerId = userid;
                        }
                    }
                    else
                    {
                        return new ApiErrorResult("传入筛查结果列表为空", Request);
                    }

                    ResResult resResult = commonBll.MutipleCreateResult(screenResultRequest);
                    return Ok<ResResult>(resResult);
                }
                else {
                    return new ApiErrorResult("传入筛查结果列表为空", Request);
                }
                
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查ID查询筛查结果
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenResultById(string scid)
        {
            IEnumerable<ScreenResultView> results = screenResultBll.GetScreenResultsByScreenId(scid);

            return Ok<IEnumerable<ScreenResultView>>(results);
        }

        /// <summary>
        /// 根据筛查ID,筛查客户ID查询筛查结果
        /// 原程序 ScreeningServiceImpl-> selectScResultByScidAndCustomerid(String scid, String customerid) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="customerid">筛查客户ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenResultsByScreenIdAndCustomerId")]
        public IHttpActionResult GetScreenResultsByScreenIdAndCustomerId(Guid scid, Guid customerid)
        {
            try
            {
                var screen = screenResultBll.GetScreenItemResultView(scid, customerid);

                return Ok(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }

        }

        /// <summary>
        /// 根据筛查ID,筛查客户ID查询筛查结果
        /// 原程序 ScreeningServiceImpl-> GetScreenResultsCode(String scid, String customerid) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="customerid">筛查客户ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenResultsCode")]
        public IHttpActionResult GetScreenResultsCode(Guid scid, Guid customerid)
        {
            try
            {
                var screen = screenResultBll.GetScreenResultsByScreenIdAndCustomerId(scid, customerid);

                return Ok(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }

        }

        /// <summary>
        /// 根据筛查ID,筛查客户ID查询筛查结果是否存在
        /// 原程序 ScreeningServiceImpl-> selectScResultByScidAndCustomerid(String scid, String customerid) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="customerid">筛查客户ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetSelectedByScreenIdAndCustomerId")]
        public IHttpActionResult GetSelectedByScreenIdAndCustomerId(Guid scid, Guid customerid)
        {
            try
            {
                IEnumerable<string> result = screenResultBll.GetSelectedByScreenIdAndCustomerId(scid, customerid);
                return Ok(result);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }

        }

        /// <summary>
        /// 根据筛查ID,筛查客户ID,筛查类别id查询筛查结果
        /// 原程序 ScreeningServiceImpl-> selectScResultByScidAndCustomerid(String scid, String customerid) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="customerid">筛查客户ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenResultsByScreenIdAndCustomerIdAndCategoryId")]
        public IHttpActionResult GetScreenResultsByScreenIdAndCustomerIdAndCategoryId(Guid scid, Guid customerid,string new_categoryid)
        {
            try
            {
                var screen = screenResultBll.GetScreenResultsByScreenIdAndCustomerIdAndCategoryId(scid, customerid, new_categoryid);
                return Ok<IEnumerable<ScreenResultView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }

        }

        /// <summary>
        /// 根据筛查ID,筛查客户ID查询筛查结果
        /// 原程序 ScreeningServiceImpl-> selectScResults(string scid, String customerid, List<string> contentids)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="customerid">筛查客户ID</param>
        /// <param name="contentids">筛查内容ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenResultsByScreenIdAndCustomerIdAndContentIds(Guid scid, Guid customerid, [FromUri] List<string> contentids)
        {
            try
            {
                var screen = screenResultBll.GetScreenResultsByScreenIdAndCustomerIdAndContentIds(scid, customerid, contentids);
                return Ok<IEnumerable<ScreenResultView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqscCRM"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("CreateSQSCCRM")]
        public IHttpActionResult CreateSQSCCRM([FromBody]CreateSCOBJCRM sqscCRM)
        {

            try
            {

                CreateSCOBJ sqsc = new CreateSCOBJ();
                sqsc.lead = sqscCRM.lead;
                sqsc.customer = sqscCRM.customer;
                sqsc.reservation = sqscCRM.reservation;
                sqsc.propaganda = sqscCRM.propaganda;
                sqsc.CreateResults = sqscCRM.CreateResults;
                UserInfo userInfo = sqscCRM.user;
                Guid userid = new Guid(userInfo.userid.ToString());
                sqsc.lead.userid = userid;
                var views = commonBll.CreateSQSC(sqsc, userInfo);
                return Ok<ResResult>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

    }
}
